package com.jd.mysql.mgr.controller.mgrdatabase;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import com.jd.mysql.mgr.dto.MgrDatabaseDTO;
import com.jd.mysql.mgr.pojo.MgrDatabase;
import com.jd.mysql.mgr.service.IMgrDatabaseService;
import com.jd.mysql.mgr.util.GlobalStaticVariables;
import com.jd.mysql.mgr.util.PageUtil;
import com.jd.mysql.mgr.util.ParamUtil;

public class MgrDatabaseController extends MultiActionController{
	public IMgrDatabaseService mgrDatabaseService;

	private static Integer limit = GlobalStaticVariables.limit;
	public void setMgrDatabaseService(IMgrDatabaseService mgrDatabaseService) {
		this.mgrDatabaseService = mgrDatabaseService;
	}
	
	public ModelAndView mgrDatabaseList(HttpServletRequest request,HttpServletResponse response) throws Exception {
		Integer machineId = ParamUtil.getIntParam(request, "machineId", 0);
		String dbName = ParamUtil.getStringParam(request,"dbName");
		String index = request.getParameter("index");
		Map<String,Object> map = new HashMap<String,Object>();
		Integer start = PageUtil.setPageParam(map,index, limit);
		Integer groupId = (Integer)request.getSession().getAttribute("group");
		Long totalCount = mgrDatabaseService.getTotalCount(true,machineId,groupId,dbName);
		List<MgrDatabase> list = mgrDatabaseService.getMgrDatabaseList(start, limit,true,machineId,groupId,dbName);
		List<MgrDatabaseDTO> dtoList = new ArrayList<MgrDatabaseDTO>();
		if(list!=null){
			for(MgrDatabase mgrdatabase : list){
				dtoList.add(mgrdatabase.toDTO());
			}
		}
		map.put("totalCount", totalCount);
		map.put("dtoList",dtoList);
		map.put("machineId",machineId);
		return new ModelAndView("database/databaseList","map",map);
	}
	
	public ModelAndView deleteMgrDatabase(HttpServletRequest request,HttpServletResponse response) throws Exception {
		Integer dbId = ParamUtil.getIntParam(request, "dbid", 0);
		MgrDatabase db = mgrDatabaseService.findMgrDatabaseById(dbId);
		if(db!=null){
			mgrDatabaseService.deleteMgrDatabase(db);
		}
		return this.mgrDatabaseList(request, response);
	}
	
	public ModelAndView disableMgrDatabase(HttpServletRequest request,HttpServletResponse response) throws Exception {
		Integer dbId = ParamUtil.getIntParam(request, "dbid", 0);
		MgrDatabase db = mgrDatabaseService.findMgrDatabaseById(dbId);
		if(db!=null){
			mgrDatabaseService.disableMgrDatabase(db);
		}
		return this.mgrDatabaseList(request, response);
	}
	
	public ModelAndView editMgrDatabase(HttpServletRequest request,HttpServletResponse response) throws Exception {
		Integer dbId = ParamUtil.getIntParam(request, "dbid", 0);
		MgrDatabase db = mgrDatabaseService.findMgrDatabaseById(dbId);
		return new ModelAndView("database/addDatabase","mgrDatabase",db);
	}
	
}
